package com.c.ctgproject.wiget.dialog

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseDialogFragment
import com.c.ctgproject.entity.service.PayTypeEntity
import com.c.ctgproject.utils.*
import com.c.ctgproject.utils.ImageUtil.changeFile
import com.nanchen.compresshelper.CompressHelper
import com.zhihu.matisse.Matisse
import kotlinx.android.synthetic.main.item_service_pay_type_dialog.*
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ServicePayTypeDialog(private val suborderId: String, private val payTyp: Int, private val mPayTypeEntity: PayTypeEntity?, private val callback: ServicePayTypeDialog) : BaseDialogFragment() {
    private val mViewModel by lazy { ViewModelProvider(this)[ServicePayTypeViewModel::class.java] }
    override fun getLayout() = R.layout.item_service_pay_type_dialog
    private var weChatFile: File? = null
    override fun viewIsCreated(view: View, savedInstanceState: Bundle?) {
        dialog?.setCanceledOnTouchOutside(false)
        when (payTyp) {
            1 -> {
                mAliPayLL.visibility = View.VISIBLE
            }
            2 -> {
                mWeChatLL.visibility = View.VISIBLE
            }
            3 -> {
                mBankLL.visibility = View.VISIBLE
            }
            4 -> {
                mMoneyLL.visibility = View.VISIBLE
            }
        }


        mPayTypeEntity?.data?.let {
            mConfirmBTN.visibility = View.GONE
            when (it.paymentMethod) {
                "1" -> {
                    mAliPayNumberET.textSet(it.aliPayAccount.handlerNull())
                    mAliPayNameET.textSet(it.aliPayName.handlerNull())
                    mAliPayNumberET.isEnabled = false
                    mAliPayNameET.isEnabled = false

                }
                "2" -> {
                    Glide.with(requireActivity()).load(it.code.handlerNull()).into(mWeChatIV)
                    mWeChatIV.isEnabled = false
                }
                "3" -> {
                    mBankNumberET.textSet(it.bankAccount.handlerNull())
                    mBankUserNameET.textSet(it.cardholder.handlerNull())
                    mBankNameET.textSet(it.openingBank.handlerNull())
                    mBankDetailNameET.textSet(it.subBranch.handlerNull())
                    mBankNumberET.isEnabled = false
                    mBankUserNameET.isEnabled = false
                    mBankNameET.isEnabled = false
                    mBankDetailNameET.isEnabled = false
                }
            }
        }
        mViewModel.error.observe(this, {
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, {
            MyLog.d("提交收款方式回调:$it")
            requireActivity().handlerHttpResult(it) {
                showToast("提交收款方式成功")
                callback.confirm()
            }
        })

        mWeChatIV.setOnClickListener {
            ImageUtil.initSingleImagePicker(requireActivity(), 1234)
        }



        mBackBTN.setOnClickListener {
            dismiss()
        }
        mConfirmBTN.setOnClickListener {
            when (payTyp) {
                1 -> {
                    val aliNumber = mAliPayNumberET.text.handlerNull()
                    val aliName = mAliPayNameET.text.handlerNull()
                    when {
                        aliNumber.isEmpty() -> showToast("请输入支付宝账号")
                        aliName.isEmpty() -> showToast("请输入支付宝名称")
                        else->{
                            mViewModel.aliPayChat(
                                handlerBody(
                                    "suborderId" to suborderId,
                                    "aliPayAccount" to aliNumber,
                                    "aliPayName" to aliName,
                                )
                            )
                        }
                    }

                }
                2 -> {
                    if (weChatFile != null) {
                        val array = ArrayList<MultipartBody.Part>()
                        val file= CompressHelper.getDefault(requireContext()).compressToFile(weChatFile!!)
                        val requestBodyImage = RequestBody.create(MediaType.parse("multipart/form-data"), file)
                        array.add(MultipartBody.Part.createFormData("wxPayCode", weChatFile!!.name, requestBodyImage))
                        val requestBodyText = MultipartBody.Part.createFormData("suborderId", suborderId)
                        array.add(requestBodyText)
                        mViewModel.payWeChat(array)
                    } else {
                        showToast("请选择微信收款码")
                    }
                }
                3 -> {
                    val bankNumber = mBankNumberET.text.handlerNull()
                    val bankDetail = mBankDetailNameET.text.handlerNull()
                    val bankUserName = mBankUserNameET.text.handlerNull()
                    val bankName = mBankNameET.text.handlerNull()
                    when {
                        bankNumber.isEmpty() -> showToast("请输入银行卡号")
                        bankDetail.isEmpty() -> showToast("请输入支行名称")
                        bankUserName.isEmpty() -> showToast("请输入持卡人姓名")
                        bankName.isEmpty() -> showToast("请输入银行名称")


                        else -> {
                            mViewModel.bank(
                                handlerBody(
                                    "suborderId" to suborderId,
                                    "bankAccount" to bankNumber,
                                    "subBranch" to bankDetail,
                                    "cardholder" to bankUserName,
                                    "openingBank" to bankName,
                                )
                            )
                        }
                    }
                }
                4 -> {
                    //现金
                    mViewModel.payMoney(suborderId)
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1234 && data != null) {
                //图片路径 同样视频地址也是这个 根据requestCode
                val mSelected = Matisse.obtainResult(data)
                MyLog.d("图片地址:$mSelected")
                if (mSelected.isNotEmpty()) {
                    weChatFile = changeFile(requireContext(), mSelected[0])
                    weChatFile?.let {
                        Glide.with(requireActivity()).load(weChatFile).into(mWeChatIV)

                    }
                }
        }
    }

    interface ServicePayTypeDialog {
        fun confirm()
    }
}